import { GTileList } from "../../../../com/mui/controls/GTileList";
import { WareHouseProxy } from "../../proxy/WareHouseProxy";
import { PackProxy } from "../../proxy/PackProxy";
import { ScrollPolicy } from "../../../../fl/controls/ScrollPolicy";
import { WareHouseItem } from "./WareHouseItem";
import { DragEvent } from "../../../../com/mui/events/DragEvent";
import { Dispatcher } from "../../../mvc/core/Dispatcher";
import { DataEvent } from "../../events/DataEvent";
import { EventName } from "../../mvc/EventName";
import { GameController } from "../../mvc/GameController";
import { ECategory } from "../../../../Message/Public/ECategory";
import { EProp } from "../../../../Message/Public/EProp";
import { Cache } from "../../cache/Cache";
import { ListEvent } from "../../../../fl/events/ListEvent";
import { DataProvider } from "../../../../fl/data/DataProvider";
import { ItemData } from "../../resource/info/ItemData";
import { BaseItem } from "../common/BaseItem";
import { EPlayerItemPosType } from "../../../../Message/Public/EPlayerItemPosType";
import { GameManager } from "../../manager/GameManager";
import { ModuleType } from "../common/ModuleType";
import { DragDropData } from "../pack/DragDropData";
type int = number;
//class WareHouseItemPanel
    
    export  class WareHouseItemPanel extends GTileList
    {
        constructor()
        {
            super();
            this._wareHouseProxy = new WareHouseProxy();
            this._packProxy = new PackProxy();
            this.initTile();
            this.doubleClickEnabled = true;
            this.addEventListener(flash.events.MouseEvent.CLICK, this.onClickHander);
            this.addEventListener(flash.events.MouseEvent.DOUBLE_CLICK, this.FastOperate);
            return;
        }

        /* internal  */initTile(): void
        {
            this.setSize(246, 205);
            this.verticalScrollPolicy = ScrollPolicy.OFF;
            this.horizontalScrollPolicy = ScrollPolicy.OFF;
            this.rowHeight = 36;
            this.columnWidth = 36;
            this.horizontalGap = 5;
            this.verticalGap = 5;
            this.setStyle("skin", new Object /* flash.display.Bitmap */());
            this.setStyle("cellRenderer", WareHouseItem);
            this.addEventListener(DragEvent.Event_Move_In, this.moveInBagHandler);
            this.addEventListener(DragEvent.Event_Throw_goods, this.throwGoodsHandler);
            return;
        }

        /* internal  */itemClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=null;
            if (arg1.target is WareHouseItem && (arg1.target as WareHouseItem).itemData) 
            {
                loc1 = (arg1.target as WareHouseItem).itemData;
                if (arg1.ctrlKey) 
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.ChatShowItem, loc1));
                }
            }
            return;
        }

        /* internal  */onClickHander(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (arg1.target is WareHouseItem && (arg1.target as WareHouseItem).itemData) 
            {
                if (!GameController.pack.view.isHide) 
                {
                    this.operate(arg1.target as WareHouseItem, 0);
                }
            }
            return;
        }

        /* internal  */FastOperate(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=null;
            var loc2=0;
            var loc3=0;
            var loc4=0;
            if (arg1.target is WareHouseItem && (arg1.target as WareHouseItem).itemData) 
            {
                if (GameController.pack.view.isHide) 
                {
                    loc1 = (arg1.target as WareHouseItem).itemData;
                    loc2 = loc1.itemInfo.item.category;
                    loc3 = loc1.itemInfo.item.type;
                    if (loc2 == ECategory._ECategoryProp && loc3 == EProp._EPropPackage) 
                    {
                        loc4 = Cache.instance.pack.wareHousePackCache.getIndexByUid(loc1.uid);
                        this.fastUsePackage(arg1.target as WareHouseItem);
                    }
                }
            }
            return;
        }

        /* internal  */ItemSelect(arg1: ListEvent): void
        {
            this.operate(WareHouseItem(this.activeCellRenderers[this.selectedIndex]), arg1.index);
            return;
        }

        /* internal  */operate(arg1: WareHouseItem, arg2: int): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.Fast_Move_wareHouseItem_ToPack, arg1));
            return;
        }

        public updateAllItem(arg1: DataProvider, arg2: Boolean=true): void
        {
            var loc2=null;
            if (arg2) 
            {
                this.dataProvider = arg1;
                return;
            }
            var loc1=0;
            while (loc1 < arg1.length) 
            {
                if (loc2 = arg1.getItemAt(loc1)) 
                {
                    this.updateItem(loc1, loc2);
                }
                ++loc1;
            }
            return;
        }

        public updateItem(arg1: int, arg2: Object): void
        {
            if (arg1 >= this._dataProvider.length || arg1 < 0) 
            {
                return;
            }
            var loc1=this._dataProvider.getItemAt(arg1) as Object;
            if (this.EqualObj(arg2, loc1)) 
            {
                return;
            }
            this._dataProvider.replaceItemAt(arg2, arg1);
            return;
        }

        /* internal  */EqualObj(arg1: Object, arg2: Object): Boolean
        {
            var loc1=arg1.hasOwnProperty("itemData");
            var loc2=arg2.hasOwnProperty("itemData");
            var loc3=arg1.hasOwnProperty("source");
            var loc4=arg2.hasOwnProperty("source");
            if (arg1 == null && arg2 == null || loc1 && loc2 && this.EqualItemData(arg1["itemData"], arg2["itemData"]) || !loc1 && !loc1 && loc3 && loc4 && arg1["source"] == arg2["source"]) 
            {
                return true;
            }
            return false;
        }

        /* internal  */EqualItemData(arg1: ItemData, arg2: ItemData): Boolean
        {
            if (arg1 && arg2 && arg1.uid == arg2.uid && arg1.itemAmount == arg2.itemAmount) 
            {
                return true;
            }
            return false;
        }

        /* internal  */moveInBagHandler(arg1: DragEvent): void
        {
            var loc1=null;
            var loc2=null;
            var loc3=null;
            var loc4=null;
            var loc5=null;
            if (this.dragable) 
            {
                loc1 = arg1.dragItem as BaseItem;
                loc2 = arg1.dropItem as BaseItem;
                loc3 = arg1.dragSouce as ItemData;
                loc4 = loc2.itemData;
                if (loc1 == loc2) 
                {
                    return;
                }
                if (loc1.posType == EPlayerItemPosType._EPlayerItemPosTypeBagExtendBar) 
                {
                    return;
                }
                if (!loc2.data["isOpened"]) 
                {
                    GameManager.instance.popupWindow(ModuleType.WareHouseBuyItem);
                    return;
                }
                if (loc3) 
                {
                    loc5 = new DragDropData(loc3.posType, loc2.posType, loc3.uid, loc1.posIndex, loc2.posIndex, loc3, loc4);
                    Dispatcher.dispatchEvent(new DataEvent(EventName.WareHouse_DragInItem, loc5));
                }
            }
            arg1.stopImmediatePropagation();
            return;
        }

        /* internal  */throwGoodsHandler(arg1: DragEvent): void
        {
            return;
        }

        /* internal  */fastUsePackage(arg1: WareHouseItem): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.Fast_USE_WareHousePackage, arg1));
            return;
        }

        /* internal */ /* var */_dictNameOperate: Map<any, any> /* flash.utils.Dictionary */;

        /* internal */ /* var */_wareHouseProxy: WareHouseProxy;

        /* internal */ /* var */_packProxy: PackProxy;

        public /* var */dragable: Boolean=true;
    }
